{% extends "tribes/base.html" %}

{% load i18n %}
{% load account_tags %}
{% load uni_form_tags %}
{% load avatar_tags %}
{% load threadedcommentstags %}
{% load pagination_tags %}
{% load extra_tagging_tags %}
{% load tribes_tags %}
{% load topics_tags %}
{% load theme_tags %}

{% block head_title %}{{ tribe.name }}{% endblock %}

{% block body %}
    
    {% if user.is_authenticated %}
        <div class="right_panel">
            <div class="members">
                <h2>{% trans "Members" %}</h2>
                <table width="100%">
                    {% for member in tribe.members.all %}
                        {% if forloop.counter0|divisibleby:"3" %}<tr>{% endif %}
                        <td>
                            <div class="avatar">{% avatar member 40 %}</div>
                            <div class="details"><a href="{% url profile_detail member.username %}" title="{{ member.username }}">{% user_display member %}</a></div>
                        </td>
                        {% if forloop.counter0|add:"1"|divisibleby:"3" %}</tr>{% endif %}
                    {% endfor %}
                    {% if tribe.members.all|length|divisibleby:"3" %}{% else %}</tr>{% endif %}
                </table>
            </div>
        </div>
    {% endif %}
    
    <div style="width: 550px;">
        
        <h1>{% trans "Tribe" %} {{ tribe.name }}</h1>
        
        <p>
            {% trans "Slug:" %} <tt>{{ tribe.slug }}</tt><br />
            {% trans "Creator:" %} <a href="{% url profile_detail tribe.creator.username %}">{% user_display tribe.creator %}</a><br />
            {% trans "Created:" %} {{ tribe.created|date }}
            <br />
            {% show_tags_for tribe %}
        </p>
        
        <p>{{ tribe.description }}</p>
        
        {% ifequal user tribe.creator %}
            <div class="form-toggle">
                <p><span id="edit-tribe-toggle">{% trans "Edit details" %}</span></p>
                
                <form class="uniForm" id="edit-tribe" method="POST" action="">
                    {% csrf_token %}
                    <fieldset class="inlineLabels">
                        {{ tribe_form|as_uni_form }}
                        <div class="form_block">
                            <input type="hidden" name="action" value="update" />
                            <input type="submit" value="{% trans "updated" %}"/>
                        </div>
                    </fieldset>
                </form>
            </div>
            
            {% ifequal tribe.members.all.count 1 %}
                <p>{% silk "delete" %} <a href="#" onclick="$('#delete_tribe_form').toggle(); return false;">{% trans "Delete tribe" %}</a><p>
                 <form class="delete_form" id="delete_tribe_form" action="{% url tribe_delete tribe.slug %}" method="POST" style="display: none;">
                     {% csrf_token %}
                     <input type="submit" value="{% trans "Delete Tribe" %}" /> (all content will be removed)
                </form>
            {% else %}
                <p>{% trans "You are not the only member of the tribe so you can't delete the tribe." %}</p>
            {% endifequal %}
            
        {% endifequal %}
        
        {% if user.is_authenticated %}
            <div>
                <form method="POST" action="">
                    {% csrf_token %}
                    {% if is_member %}
                        {% ifequal user tribe.creator %}
                            <p>{% trans "You are the creator of this tribe so can't leave (yet)." %}</p>
                        {% else %}
                            <input type="hidden" name="action" value="leave" />
                            <input type="submit" value="{% trans "leave" %}"/>
                        {% endifequal %}
                    {% else %}
                        <input type="hidden" name="action" value="join" />
                        <input type="submit" value="{% trans "join tribe" %}"/>
                    {% endif %}
                </form>
            </div>
        {% else %}
            {% url acct_signup as signup_url %}
            {% url acct_login as login_url %}
            <p>{% blocktrans %}<a href="{{ signup_url }}">Sign up</a> and <a href="{{ login_url }}">log in </a> to join this tribe.{% endblocktrans %}</p>
        {% endif %}
        
        
        {% if user.is_authenticated %}
            <h2>{% trans "Discussion Topics" %}{% if tribe.topics.all.count %} ({{ tribe.topics.all.count }}){% endif %}</h2>
            
            {% with tribe.topic_set.all|slice:":5" as topics %}
                {% if topics %}
                    {% for topic in topics|slice:":5" %}
                        {% show_topic topic %}
                    {% endfor %}
                    <p><a href="{% url topic_list tribe.slug %}">{% trans "more..." %}</a></p>
                {% else %}
                    {% url topic_create tribe.slug as new_topic_url %}
                    <p>{% blocktrans %}None yet. <a href="{{ new_topic_url }}">Start one...</a>{% endblocktrans %}</p>
                {% endif %}
            {% endwith %}
            
                      
        {% endif %}
    </div>

{% endblock %}

{% block extra_body %}
    <script type="text/javascript">
        $(document).ready(function() {
            $('#edit-tribe').hide();
            $('#edit-tribe-toggle').click(function() {
                $('#edit-tribe').toggle();
                $('#edit-tribe').autoscroll();
                return false;
            });
            if ($('#edit-tribe .error').length) {
                $('#edit-tribe').show();
                $('#edit-tribe .error').autoscroll();
            }
        });
    </script>
    
{% endblock %}
